/*function $(elementId){
    return document.getElementById(elementId).value;
}*/
var isExist = true;
function divId(elementId){
    return document.getElementById(elementId);
}

/*用户名验证*/
// function checkUser(){
//     var user=document.getElementById("user").value;
//     var userId=divId("user_prompt");
//     userId.innerHTML="";
//     var reg=/^[a-zA-Z][a-zA-Z0-9]{3,15}$/;
//     if(reg.test(user)==false){
//         console.log(1);
//         userId.innerHTML="<span style='color: red'>用户名不正确</span>";
//         return false;
//     }else {
//         var username=$(this).val();
//         console.log(username);
//         if (username != ""){
//             $.ajax({
//                 url:"localhost:8081/project/selectName.do",
//                 contentType:"application/json; charset=utf-8",
//                 dataType:"json",
//                 async:true,
//                 data:JSON.stringify({"username":username}),
//                 type:"POST",
//                 success:function (data) {
//                     if (data.code == 1){
//                         $('#user_prompt').css("color","green");
//                         $('#user_prompt').html("该用户可以注册");
//                     }else {
//                         $('#user_prompt').css("color","red");
//                         $('#user_prompt').html("该用户已经注册");
//                     }
//                 },
//             });
//         }
//     }
//     return true;
// }

        /*密码验证*/
        function checkPwd() {
            var pwd = document.getElementById("pwd").value;
            var pwdId = divId("pwd_prompt");
            pwdId.innerHTML = "";
            var reg = /^[a-zA-Z0-9]{4,10}$/;
            if (reg.test(pwd) == false) {
                pwdId.innerHTML = "<span style='color: red'>密码不能含有非法字符，长度在4-10之间</span>";
                return false;
            }
            return true;
        }

        function checkRepwd() {
            var repwd = document.getElementById("repwd").value;
            var pwd = document.getElementById("pwd").value;
            var repwdId = divId("repwd_prompt");
            repwdId.innerHTML = "";
            if (pwd != repwd) {
                repwdId.innerHTML = "<span style='color: red'>两次输入的密码不一致</span>";
                return false;
            }
            return true;
        }


        $("body").on("blur","#user",function checkName(){
            var user=document.getElementById("user").value;
            var userId=divId("user_prompt");
            userId.innerHTML="";
            var reg=/^[a-zA-Z][a-zA-Z0-9]{3,15}$/;
            if(reg.test(user)==false){
                console.log(1);
                userId.innerHTML="<span style='color: red'>用户名不正确</span>";
                // return false;
            }else {
                var username=$(this).val();
                console.log(username);
                if (username != ""){
                    $.ajax({
                        url:"http://localhost:8080/selectName",
                        contentType:"application/json; charset=utf-8",
                        dataType:"json",
                        async:true,
                        data:JSON.stringify({"userName":username}),
                        type:"POST",
                        success:function (data) {
                            console.log(data.code)
                            if (data.code == 1){
                                isExist = false;
                                $('#user_prompt').css("color","green");
                                $('#user_prompt').html("该用户可以注册");
                            }else {
                                isExist = true;
                                $('#user_prompt').css("color","red");
                                $('#user_prompt').html("该用户已经注册");
                            }
                        },
                    });
                }else {
                    alert("请输入用户名")
                    $('#submit').attr("disabled","disabled");
                }
            }
            // return true;
        })

function judgeAll() {
    if(isExist){
        return false
    }
    if(checkPwd() && checkRepwd() && checkName()){
        return true
    }
    return false
}

// $('#user').blur(function () {
//
// })

/*
/!*验证邮箱*!/
function checkEmail(){
    var email=$("email");
    var email_prompt=divId("email_prompt");
    email_prompt.innerHTML="";
    var reg=/^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/;
    if(reg.test(email)==false){
        email_prompt.innerHTML="<span style='color: red'>Email格式不正确，例如web@sohu.com</span>";
        return false;
    }
    return true;
}
/!*验证手机号码*!/
function checkMobile(){
    var mobile=$("mobile");
    var mobileId=divId("mobile_prompt");
    var regMobile=/^1\d{10}$/;
    if(regMobile.test(mobile)==false){
        mobileId.innerHTML="<span style='color: red'>手机号码不正确，请重新输入</span>";
        return false;
    }
    mobileId.innerHTML="";
    return true;
}*/
